package com.jinyuan.options;

import com.jinyuan.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 主页面底部菜单控制
 * @author azuryleaves
 *
 */
public class MainBottomFragment extends Fragment {
	
	private Callbacks callbacks = defaultCallbacks;
	public static RadioGroup main_tab_RadioGroup;
	private RadioButton main_tab_1_RadioButton, main_tab_2_RadioButton;
	private View main_tab_circle_RelativeLayout;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.main_tab_bottom, container, false);
		main_tab_RadioGroup = (RadioGroup) view.findViewById(R.id.main_tab_RadioGroup);
		main_tab_1_RadioButton = (RadioButton) view.findViewById(R.id.main_tab_1_RadioButton);
		main_tab_2_RadioButton = (RadioButton) view.findViewById(R.id.main_tab_2_RadioButton);
		main_tab_circle_RelativeLayout = view.findViewById(R.id.main_tab_circle_RelativeLayout);
		main_tab_circle_RelativeLayout.setOnClickListener(clickListener);
		main_tab_1_RadioButton.setOnClickListener(clickListener);
		main_tab_2_RadioButton.setOnClickListener(clickListener);
		callbacks.onRadioButtonClick(main_tab_1_RadioButton);
		main_tab_1_RadioButton.setChecked(true);
		return view;
	}

	private OnClickListener clickListener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.main_tab_circle_RelativeLayout:
				callbacks.onCircleViewClick(view);
				break;
				
			default:
				callbacks.onRadioButtonClick(view);
				break;
			}
		}
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof Callbacks)) {
			throw new IllegalStateException(
					"Activity must implement fragment's callbacks.");
		}
		callbacks = (Callbacks) activity;
	}

	@Override
	public void onDetach() {
		super.onDetach();
		callbacks = defaultCallbacks;
	}

	public interface Callbacks {
		
		/**
		 * 底部菜单RadioButton点击事件回调接口
		 * @param view
		 */
		public void onRadioButtonClick(View view);
		
		/**
		 * 底部菜单CircleFrameLayout点击事件回调接口
		 * @param view
		 */
		public void onCircleViewClick(View view);

	}

	private static Callbacks defaultCallbacks = new Callbacks() {

		@Override
		public void onCircleViewClick(View view) {
			
		}

		@Override
		public void onRadioButtonClick(View view) {
			
		}

	};
}
